package com.danco.training.csvworker;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.danco.training.view.Separator;


public class CSVReader {
	
	
	public static String[] readFile(String file){
		
		BufferedReader br = null;
		String line = "";
		String cvsSplitBy = Separator.SEPARATOR.toString();
		String[] entities = null;
		
		try {
	 
			br = new BufferedReader(new FileReader(file));
			while ((line = br.readLine()) != null) {
	 
			       
				 entities = line.split(cvsSplitBy);
				
				 
			}
			
			
	 
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return entities;
	 
	  }
	
	public static boolean existedEntity(String file, int id){
		BufferedReader br = null;
		String line = "";
		String cvsSplitBy = Separator.SEPARATOR.toString();
		String[] entities = null;
	 
		try {
	 
			br = new BufferedReader(new FileReader(file));
			while ((line = br.readLine()) != null) {
	 
			       
				 entities = line.split(cvsSplitBy);
				 if (Integer.parseInt(entities[0]) == id){
					 return true;
				 }
				 
			}
			
			
	 
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return false;
	 
	}

}
